package demo.spring.data.flow.b;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lushun.chen on 2017/3/30.
 *
 * @author lushun.chen
 */
@EnableBinding(Source.class)
@Component
public class LogingSource {

    @Autowired
    private Source source;
    @Autowired
    private ObjectMapper objectMapper;

    @Scheduled(fixedDelay = 1500)
    public void timerEntitySource() {
        Map<String, Object> map = new HashMap<>();
        map.put("date", new Date());
        map.put("msg", "我是一个兵");
        try {
            source.output().send(MessageBuilder.withPayload(objectMapper.writeValueAsString(map)).build());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
